package com.freddy.sqlite.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.freddy.sqlite.main.model.User;

import java.util.List;

/**
 * Created by Freddy on 2015/12/3.
 * chenshichao@outlook.com
 * 用户表
 */
public class UserDB {

    private final static String TAG = UserDB.class.getSimpleName();

    private static UserDB INSTANCE;

    private static DataBaseHelper mDBHelper;

    /*
     * 正式表名
     */
    private static final String TABLE_NAME = UserDB.class.getSimpleName();

    /**
     * 单例（双重校检锁）
     * @param context
     * @return
     */
    public static UserDB getInstance(Context context) {
        if(null == INSTANCE) {
            synchronized (UserDB.class) {
                if(null == INSTANCE) {
                    INSTANCE = new UserDB();
                    mDBHelper = DataBaseHelper.getInstance();
                }
            }
        }

        return INSTANCE;
    }

    /**
     * 私有构造方法，避免外部直接对本类进行实例化
     */
    private UserDB() {
    }

    public void create() {
        mDBHelper.createTable(TABLE_NAME, UserTableColumns.getColumns());
    }

    public void drop() {
        mDBHelper.dropTable(TABLE_NAME);
    }

    /**
     * 表数据迁移
     */
    public void migrate() {
        mDBHelper.migrateTable(TABLE_NAME, mDBHelper.getTempTableName(TABLE_NAME), UserTableColumns.getColumns());
    }

    /**
     * 插入一条数据
     * @param user
     */
    public void insert(User user) {
        ContentValues cv = new ContentValues();
        cv.put(UserTableColumns.userName, user.getUserName());
        cv.put(UserTableColumns.phoneNumber, user.getPhoneNumber());
        cv.put(UserTableColumns.sex, user.getSex());
        cv.put(UserTableColumns.birthday, user.getBirthday());
        cv.put(UserTableColumns.province, user.getProvince());
        cv.put(UserTableColumns.city, user.getCity());
        cv.put(UserTableColumns.testMigrate, user.getTestMigrate());

        mDBHelper.insert(TABLE_NAME, cv);
    }

    /**
     * 查询所有用户数据，根据id降序
     * @return
     */
    public List<User> queryAll() {
        Cursor cursor = mDBHelper.query(TABLE_NAME, DataBaseHelper.DataBaseColumns.id, false);
        List<User> userList = mDBHelper.cursorToList(cursor, User.class);
        if(null != userList) {
            Log.d(TAG, "count = " + userList.size());
            return userList;
        }else {
            Log.e(TAG, "cursor is null!");
        }

        return null;
    }

    /**
     * 判断UserDB表是否存在数据
     * @return
     */
    public boolean hasData() {
        return mDBHelper.getCountByTableName(TABLE_NAME) > 0;
    }

    static class UserTableColumns extends DataBaseHelper.DataBaseColumns {
        public static final String userName = "userName";        // 用户名
        public static final String phoneNumber = "phoneNumber";  // 手机号码
        public static final String sex = "sex";                  // 性别
        public static final String birthday = "birthday";        // 生日
        public static final String province = "province";        // 所在省
        public static final String city = "city";                // 所在市
        public static final String testMigrate = "testMigrate";  // 测试数据库迁移列

        public static String[] getColumns() {
            return new String[]{
                    id,
                    userName,
                    phoneNumber,
                    sex,
                    birthday,
                    province,
                    city,
                    testMigrate
            };
        }
    }

    /**
     * 插入测试数据
     */
    public void insertTestData() {
        User user = new User();
        user.setUserName("FreddyChen1");
        user.setPhoneNumber("13729189671");
        user.setSex("F");
        user.setBirthday("1993-01-19");
        user.setProvince("广西");
        user.setCity("桂林");
        user.setTestMigrate("测试数据库迁移");

        insert(user);
    }
}
